/**
 * 
 */
package com.worksmap.core.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TraderateAddRequest;
import com.taobao.api.response.TraderateAddResponse;
import com.worksmap.core.constant.RateConstant;
import com.worksmap.core.entity.TaoBaoSeller;
import com.worksmap.core.entity.TaoBaoTrade;
import com.worksmap.core.entity.Trade;
import com.worksmap.core.service.TradeService;
import com.worksmap.core.service.RateService;
import com.worksmap.core.support.TaoBaoClientFactory;
import com.worksmap.core.util.DateUtil;

/**
 * @author jack
 *
 */
@Service
public class RateServiceImpl implements RateService {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	TradeService tradeService;
	
	@Override
	public boolean rateOrder(TaoBaoSeller seller, String startTime, String endTime){
		Date beginDate = DateUtil.StringToDate(startTime, "yyyy-MM-dd HH:mm:ss");
		Date endDate = DateUtil.StringToDate(endTime, "yyyy-MM-dd HH:mm:ss");
		List<TaoBaoTrade> tradeList = tradeService.getSellerNotRateTradeList(seller, beginDate, endDate);   //获取买家已评价，但是卖家未评价订单
		logger.info(tradeList);
		boolean stats = false;
		stats = rateAllTrader(tradeList);
		return stats;
	}
	
	private boolean rateAllTrader(List<TaoBaoTrade> tradeList){
		TaobaoClient client = new DefaultTaobaoClient(RateConstant.apiUrl, RateConstant.appkey, RateConstant.appSecret);
		TraderateAddRequest req = new TraderateAddRequest();   //新增单个评价
//		TraderateListAddRequest req = new TraderateListAddRequest();    //针对父子订单新增批量评价
		for(TaoBaoTrade trade : tradeList){
			req.setTid(Long.valueOf(trade.getTid()));
//			req.setOid(1234L);
			req.setResult("good");  //评价结果,可选值:good(好评),neutral(中评),bad(差评)
			req.setRole("seller");	//评价者角色,可选值:seller(卖家),buyer(买家)
			req.setContent("很好,很快");
			req.setAnony(true);   //是否匿名,卖家评不能匿名。可选值:true(匿名),false(非匿名)
			try {
				TraderateAddResponse response = client.execute(req , RateConstant.sessionKey);     //新增单个评价
//			TraderateListAddResponse response = client.execute(req , sessionKey);   //针对父子订单新增批量评价
				logger.info("返回的订单评价:"+response.getBody());
			} catch (ApiException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.worksmap.core.service.RateService#rateOneTrade()
	 */
	@Override
	public boolean rateOneTrade(Trade trade) {
		TaobaoClient client = TaoBaoClientFactory.getClient();
		TraderateAddRequest req = new TraderateAddRequest();   //新增单个评价
		return rateTrade(trade, client, req);
	}

	/* (non-Javadoc)
	 * @see com.worksmap.core.service.RateService#rateTradeList()
	 */
	@Override
	public boolean rateTradeList(List<Trade> tradeList) {
		TaobaoClient client = TaoBaoClientFactory.getClient();
		TraderateAddRequest req = new TraderateAddRequest();   //新增单个评价
		for(Trade trade : tradeList){
			rateTrade(trade, client, req);
		}
		return true;
	}
	
	private boolean rateTrade(Trade trade, TaobaoClient client, TraderateAddRequest req){
		req.setTid(Long.valueOf(trade.getTid()));
		req.setResult("good");  //评价结果,可选值:good(好评),neutral(中评),bad(差评)
		req.setRole("seller");	//评价者角色,可选值:seller(卖家),buyer(买家)
		req.setContent("很好,很快");
		req.setAnony(true);   //是否匿名,卖家评不能匿名。可选值:true(匿名),false(非匿名)
		try {
			TraderateAddResponse response = client.execute(req , RateConstant.sessionKey);     //新增单个评价
			logger.info("返回的订单评价:"+response.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return true;
	}

}
